<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo-12-列表渲染</title>
    <script type="text/javascript" src="../JS/vue.js"></script>
</head>
<body>

<div id="root">
    <!-- 遍历数组 -->
    <h3>人员列表</h3>
    <input type="text" placeholder="请输入名字" v-model="keyWord">
    <ul>
        <!-- 可以使用 persons 列表中的 id 作为遍历时的 key -->
        <li v-for="(person, index) in persons" v-show="person.ifShow" :key="person.id">
            {{ index + 1 }}. {{ person.name }}: {{ person.age }} | {{person.sex}}
        </li>
    </ul>
</div>

<script text="text/javascript">
    const vm = new Vue({
        el: '#root',

        data: {
            keyWord: '',

            persons: [{
                id: '10001',
                name: '马冬梅',
                age: 19,
                sex: '女',
                ifShow: true,
            }, {
                id: '10002',
                name: '周冬雨',
                age: 23,
                sex: '女',
                ifShow: true,
            }, {
                id: '10003',
                name: '周杰伦',
                age: 21,
                sex: '男',
                ifShow: true,
            }, {
                id: '10004',
                name: '温兆伦',
                age: 22,
                sex: '男',
                ifShow: true,
            },],
        },

        watch: {
            keyWord: {
                immediate: false,

                handler(value) {
                    for (let i = 0; i < this.persons.length; i++) {
                        this.persons[i].ifShow = !!this.persons[i].name.includes(value);
                    }
                },
            },
        },
    })
</script>
</body>
</html>